# -*- coding:utf-8 -*-
import bpy


class BasicFuncs(object):

    def __init__(self):
        pass

    def selectAll(self):
        bpy.ops.object.select_all(action='SELECT')

    def deselectAll(self):
        bpy.ops.object.select_all(action='DESELECT')

    def selectObj(self, name):
        self.deselectAll()
        bpy.data.objects[name].select = True

    def deleteObj(self, name):
        self.selectObj(name)
        bpy.ops.object.delete(use_global=False)

    def hide3DViewFloor(self, hide=True):
        for a in bpy.data.window_managers[0].windows[0].screen.areas:
            if a.type == 'VIEW_3D':
                area = a
                break

        if area:
            space = area.spaces[0]
        else:
            space = bpy.context.space_data

        space.show_floor = not hide

    def leftMouseSelect(self):
        bpy.context.user_preferences.inputs.select_mouse = 'LEFT'

    # 一开始就将透视方式改为ortho
    def viewOrtho(self):
        for area in bpy.context.screen.areas:
            if area.type == 'VIEW_3D':
                ctx = bpy.context.copy()
                ctx['area'] = area
                ctx['region'] = area.regions[-1]

                # 一开始就将透视方式改为ortho
                bpy.ops.view3d.view_persportho(ctx)

    # 一开始就显示所有物体，即home键效果
    def viewAll(self):
        for area in bpy.context.screen.areas:
            if area.type == 'VIEW_3D':
                ctx = bpy.context.copy()
                ctx['area'] = area
                ctx['region'] = area.regions[-1]

                # 一开始就将透视方式改为ortho
                bpy.ops.view3d.view_all(ctx)

    # 隐藏游标
    def hidemaniputor(self, hide=True):
        for a in bpy.data.window_managers[0].windows[0].screen.areas:
            if a.type == 'VIEW_3D':
                area = a
                break

        if area:
            space = area.spaces[0]
        else:
            space = bpy.context.space_data

        space.show_manipulator = False

    # 调背景的亮度
    def setFlattyLight(self):
        bpy.ops.script.execute_preset(filepath="./flatty_light.xml", menu_idname="USERPREF_MT_interface_theme_presets")

    # vad初始化操作
    def nodeEditorInit(self):
        bpy.context.scene.use_nodes = True
        tree = bpy.context.scene.node_tree

        # clear default nodes
        for node in tree.nodes:
            tree.nodes.remove(node)
